const {
    ordersModel
} = require('./models/ordersModel')
module.exports = {
    // 通过出租方id获取他的所有车位
    getOrder: async (lessorsId) => {
        console.log('成功进入第三层获取订单数据', lessorsId)
        let total = 0 // 存放总条数
        let orders = [] // 存放数据
        if (lessorsId.state) {
            total = (await ordersModel.find({
                lessorsId: lessorsId.lessorsId,
                state: lessorsId.state
            })).length;
            console.log(total)
            orders = await ordersModel.find({ lessorsId: lessorsId.lessorsId, state: lessorsId.state })
                .populate('tenantsId')
                .populate('parkingsId')
                .populate('lessorsId')
                .limit(lessorsId.pageSize - 0) //限制的条数
                .skip((lessorsId.currentPage - 1) * lessorsId.pageSize) //跳过多少页;
        } else {
            total = (await ordersModel.find({
                lessorsId: lessorsId.lessorsId,
            })).length;
            orders = await ordersModel.find({ lessorsId: lessorsId.lessorsId })
                .populate('tenantsId')
                .populate('parkingsId')
                .populate('lessorsId')
                .limit(lessorsId.pageSize - 0) //限制的条数
                .skip((lessorsId.currentPage - 1) * lessorsId.pageSize) //跳过多少页;
        }
        const pages = Math.ceil(total / lessorsId.pageSize)
        // console.log(orders)
        return {
            orders,
            total,
            pages
        };
    },
    searchOrder: async (lessorsId) => {
        console.log('成功进入第三层  搜索  订单数据')
        const total = (await ordersModel.find({
            lessorsId: lessorsId.lessorsId,
        })).length;
        const pages = Math.ceil(total / lessorsId.pageSize)
        const orders = await ordersModel.find({ lessorsId: lessorsId.lessorsId })
            .populate('tenantsId')
            .populate('parkingsId')
            .populate('lessorsId')
            .limit(lessorsId.pageSize - 0) //限制的条数
            .skip((lessorsId.currentPage - 1) * lessorsId.pageSize) //跳过多少页;
        console.log(orders)
        return {
            orders,
            total,
            pages
        };
    },
}